<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <form action="http://www.baidu.com" method="POST" id="login">
    <input type="text" name="username">
    <input type="passowrd" name="password">
    <input type="submit" id="btn" value="登录">
  </form>
  <script>
    localStorage.setItem('name','a')
    console.log(localStorage.length);
    //获取不存在的返回null
    console.log(localStorage.getItem('name')); 
    //删除不存在key不报错
    localStorage.removeItem('name')
    localStorage.clear()
    //使用localStorage实现自动填充
    const btn=document.getElementById('btn')
    const loginForm=document.getElementById('login')
    const username=localStorage.getItem('username')
    if(username){
      loginForm.username.value=username
    }
    btn.onclick=function(e){
      e.preventDefault();
      localStorage.setItem('username',loginForm.username.value)
      loginForm.submit()
    }
  </script>
</body>
</html>